package com.leyou.item.web;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.service.SpecService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("spec")
public class SpecController {

    private final SpecService specService;

    public SpecController(SpecService specService) {
        this.specService = specService;
    }


    /**
     * 根据分类ID查询参数组合
     *
     * @param cid
     * @return
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> listGroupByCategory(@RequestParam("id") Long cid) {
        return ResponseEntity.ok(this.specService.listGroupByCategory(cid));
    }

    /**
     * 根据组Id查询对应的规格参数集合
     *
     * @param gid
     * @return
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> listSpecParams(
            @RequestParam(value = "categoryId", required = false) Long cid,
            @RequestParam(value = "searching", required = false) Boolean searching,
            @RequestParam(value = "groupId", required = false) Long gid
    ) {

        return ResponseEntity.ok(this.specService.listSpecParams(gid, cid, searching));
    }

    /**
     * 规格参数组新增
     *
     * @param specGroupDTO
     * @return
     */
    @PostMapping("/group")
    public ResponseEntity<Void> addSpecGroup(@RequestBody SpecGroupDTO specGroupDTO) {
        this.specService.addSpecGroup(specGroupDTO);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 规格参数组修改
     * @param specGroupDTO
     * @return
     */
    @PutMapping("group")
    public ResponseEntity<Void> updteSpecGroup(@RequestBody SpecGroupDTO specGroupDTO) {
        this.specService.updteSpecGroup(specGroupDTO);
        return ResponseEntity.ok().build();
    }
    /**
     * 规格参数新增
     * @param specParamDTO
     * @return
     */
    @PostMapping("/param")
    public ResponseEntity<Void> addSpecParam(
            @RequestBody SpecParamDTO specParamDTO){

        this.specService.addSpecParam(specParamDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    /**
     * 规格参数修改
     * @param specParamDTO
     * @return
     */
    @PutMapping("/param")
    public ResponseEntity<Void> updateSpecParam(
            @RequestBody SpecParamDTO specParamDTO){

        this.specService.updateSpecParam(specParamDTO);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据分类查询规格参数组集合以及组内参数集合
     * @param cid
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> listSpecGroupWithSpecParam(
            @RequestParam("id")Long cid){

        return ResponseEntity.ok(this.specService.listSpecGroupWithSpecParam(cid));
    }
}
